from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Autor(models.Model):
    nome = models.CharField(max_length=40)
    sobrenome = models.CharField(max_length=50)
    def __unicode__(self):
        return self.sobrenome.upper() + ", " + self.nome[0] + "."

class Livro(models.Model):
    titulo = models.CharField(max_length=200)
    numero_paginas = models.IntegerField()
    editora = models.CharField(max_length=100)
    local = models.CharField(max_length=100)
    edicao = models.IntegerField()
    volume = models.IntegerField(blank=True)
    autor = models.ManyToManyField(Autor)
    def __unicode__(self):
        return self.titulo

class Tag(models.Model):
    nome_tag = models.CharField(max_length=80)
    def __unicode__(self):
        return self.nome_tag
    
class Citacao(models.Model):
    livro = models.ForeignKey(Livro)
    citacao = models.TextField()
    numero_pagina = models.IntegerField()
    comentario = models.TextField()
    tags = models.ManyToManyField(Tag)
    owner = models.ForeignKey(User,related_name='dono')
    datecreated = models.DateTimeField(editable=False,auto_now=True,auto_now_add=True)
    public = models.BooleanField(default=True)
    sharedwith = models.ManyToManyField(User,blank=True)

    def __unicode__(self):
        i = self.citacao.split().__iter__()
        rstring = "\"" + i.next() + " "
        rstring += i.next() + " "
        rstring += i.next() + " "
        rstring += i.next() + " "
        rstring += i.next() + " [...] "
        nlist = self.citacao.split()
        nlist = nlist[-6:-1]
        i = nlist.__iter__()
        rstring += i.next() + " "
        rstring += i.next() + " "
        rstring += i.next() + " "
        rstring += i.next() + " "
        #rstring += i.next() + " "
        rstring += i.next() + "\", "
        rstring += self.livro.titulo.upper()
        return rstring